package cn.hls.web.controller.user;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.hls.entity.Module;
import cn.hls.entity.User;
import cn.hls.entity.vo.ModuleDTO;
import cn.hls.service.ACLService;
import cn.hls.service.CarService;
import cn.hls.service.CompanyService;
import cn.hls.service.MemberService;
import cn.hls.web.controller.base.BaseController;

@SuppressWarnings("rawtypes")
@Controller
public class MainController extends BaseController {
	@Resource(name = "aclService")
	private ACLService aclService;
	@Resource(name="memberService")
	private MemberService memberService;
	@Resource(name="carService")
	private CarService carService;
	@Resource(name="companyService")
	private CompanyService companyService;
	@RequestMapping("/main")
	public ModelAndView execute(HttpServletRequest request) throws Exception {
		// to－do通过request获取session中的user对象的角色信息，然后读取该角色所拥有的权限信息
		User user = getCurrentUser(request);
		List<Module> moduleList = aclService.searchModules(user.getId());
		return new ModelAndView("main").addObject("moduleList", moduleList);
	}

	@RequestMapping("index")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		ModelAndView modelAndView = new ModelAndView("index");
		User user = (User) request.getSession().getAttribute("currentUser");
		Map<String,Integer>map=memberService.loadExpiredMembers(user.getPerson().getCompanyId());//汇总统计快过期的员工相关证书
		List<ModuleDTO> list = null;
		if(map!=null && !map.isEmpty()){//存在数据情况
			list=new ArrayList<ModuleDTO>();
			if(map.get("driverLiceseNum")!=null){//驾驶员证及驾驶员资格证
				list.add(new ModuleDTO("驾驶证", "/member/view.action?pa=driverLiceseNum",map.get("driverLiceseNum")));
			}
			if(map.get("driverQuaCertNum")!=null){//驾驶资格从业证
				list.add(new ModuleDTO("驾驶员资格证", "/member/view.action?pa=driverQuaCertNum",map.get("driverQuaCertNum")));
			}
			if(map.get("superCargoNum")!=null){//押运员从业证
				list.add(new ModuleDTO("押运从业资格证", "/member/view.action?pa=superCargoNum",map.get("superCargoNum")));
			}
			if(map.get("safetyOfficerNum")!=null){//安管员资格从业证
				list.add(new ModuleDTO("安管员资格从业证", "/member/view.action?pa=safetyOfficerNum",map.get("safetyOfficerNum")));
			}
			if(map.get("mainPersonResNum")!=null){//主要负责人从业资格证
				list.add(new ModuleDTO("主要负责人从业资格证", "/member/view.action?pa=mainPersonResNum",map.get("mainPersonResNum")));
			}
			if(map.get("loadUnloadManNum")!=null){//装卸管理从业资格证
				list.add(new ModuleDTO("装卸管理从业资格证", "/member/view.action?pa=loadUnloadManNum",map.get("loadUnloadManNum")));
			}
		}
		modelAndView.addObject("moduleList", list);
		
		//企业相关证件过期汇总
		Map<String,Integer>companyMap=companyService.loadExpiredCompany(user.getPerson().getCompanyId());
		List<ModuleDTO>companyModuleList=null;
		if(companyMap!=null && !companyMap.isEmpty()){//企业存在过期数据
			companyModuleList=new ArrayList<ModuleDTO>();
			//营业执照(工商注册号)
			if(companyMap.get("bussinessRegiste")!=null){
				companyModuleList.add(new ModuleDTO("营业执照", "/company/carryView.action?pa=bussinessRegiste&type=1",companyMap.get("bussinessRegiste")));
			}
			//经营许可证
			if(companyMap.get("bussinessLicense")!=null){
				companyModuleList.add(new ModuleDTO("经营许可证", "/company/carryView.action?pa=bussinessLicense&type=1",companyMap.get("bussinessLicense")));
			}
		}
		modelAndView.addObject("companyModuleList",companyModuleList);
		//车辆相关证件过期汇总
		Map<String,Integer>carMap=carService.loadExpiredCar(user.getPerson().getCompanyId());//汇总统计快过期的车辆相关证件
		List<ModuleDTO>carModuleList=null;
		if(carMap!=null && !carMap.isEmpty()){
			carModuleList=new ArrayList<ModuleDTO>();
			if(carMap.get("forcedInsurance")!=null){//强制保险过期汇总统计
				carModuleList.add(new ModuleDTO("强制保险", "/car/view.action?pa=forcedInsuranceTime",carMap.get("forcedInsurance")));
			}
			if(carMap.get("thirdInsurance")!=null){//第三责任险
				carModuleList.add(new ModuleDTO("第三责任险", "/car/view.action?pa=thirdInsuranceTime",carMap.get("forcedInsurance")));
			}
			if(carMap.get("carrierInsurance")!=null){//承运人责任险
				carModuleList.add(new ModuleDTO("承运人责任险", "/car/view.action?pa=carrierInsuranceTime",carMap.get("carrierInsurance")));
			}
			if(carMap.get("driveExam")!=null){//车辆行驶证年审
				carModuleList.add(new ModuleDTO("车辆行驶证年审", "/car/view.action?pa=driveExamTime",carMap.get("driveExam")));
			}
			if(carMap.get("transLicenseExam")!=null){//道路运输证年审
				carModuleList.add(new ModuleDTO("道路运输证年审", "/car/view.action?pa=transLicenseExamTime",carMap.get("transLicenseExam")));
			}
			if(carMap.get("tankExam")!=null){//罐体年检
				carModuleList.add(new ModuleDTO("罐体年检", "/car/view.action?pa=tankExamTime",carMap.get("tankExam")));
			}
			if(carMap.get("techicalGrade")!=null){//技术等级评定
				carModuleList.add(new ModuleDTO("技术等级评定", "/car/view.action?pa=techicalGradeTime",carMap.get("techicalGrade")));
			}
			if(carMap.get("secondMaintenance")!=null){//二级维护
				carModuleList.add(new ModuleDTO("二级维护", "/car/view.action?pa=secondMaintenanceTime",carMap.get("secondMaintenance")));
			}
		}
		modelAndView.addObject("carModuleList",carModuleList);
		return modelAndView;
	}

}
